package server.messageContent;

import common.Constants;
import common.DeserializationManager;
import common.data.CreateAccountAgrs;
import common.interfaces.MessageContent;

public class CreateAccountMsgContent extends CreateAccountAgrs implements MessageContent {
	private  byte[] data;
	private int length;
	
	public CreateAccountMsgContent(byte[] data){
		
		this.data = data;
		this.length = data.length;
		
		int currentIndex = 0;
		byte[] holderNameLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, holderNameLengthStream, 0, Constants.INT_LENGTH);
		int holderNameLength = DeserializationManager.getCurrent().toInt(holderNameLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] holderNameStream = new byte[holderNameLength];
		System.arraycopy(data, currentIndex, holderNameStream, 0, holderNameLength);
		this.holderName = DeserializationManager.getCurrent().toString(holderNameStream);
		currentIndex += holderNameLength;
		
		byte[] passwordLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, passwordLengthStream, 0, Constants.INT_LENGTH);
		int passwordLength = DeserializationManager.getCurrent().toInt(passwordLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] passwordStream = new byte[passwordLength];
		System.arraycopy(data, currentIndex, passwordStream, 0, passwordLength);
		this.password = DeserializationManager.getCurrent().toString(passwordStream);
		currentIndex += passwordLength;

		byte[] currencyLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, currencyLengthStream, 0, Constants.INT_LENGTH);
		int currencyLength = DeserializationManager.getCurrent().toInt(currencyLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] currencyStream = new byte[currencyLength];
		System.arraycopy(data, currentIndex, currencyStream, 0, currencyLength);
		this.currency = DeserializationManager.getCurrent().toCurrencyEnum(currencyStream);
		currentIndex += currencyLength;

		byte[] balanceLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, balanceLengthStream, 0, Constants.INT_LENGTH);
		int balanceLength = DeserializationManager.getCurrent().toInt(balanceLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] balanceStream = new byte[balanceLength];
		System.arraycopy(data, currentIndex, balanceStream, 0, balanceLength);
		this.balance = DeserializationManager.getCurrent().toFloat(balanceStream);
		currentIndex += balanceLength;
	}

	@Override
	public byte[] getByte() {
		return this.data;
	}

	@Override
	public int getLength() {
		return this.length;
	}
}
